//
//  LCViewController+LCBaiduMobAdPrerollNative.m
//  BaiduAd_Example
//
//  Created by admin on 2018/6/27.
//  Copyright © 2018年 iOS11luchao. All rights reserved.
//

#import "LCViewController+LCBaiduMobAdPrerollNative.h"
#import "BaiduMobAdSDK/BaiduMobAdPrerollNativeAdObject.h"
#import "BaiduMobAdSDK/BaiduMobAdNativeVideoView.h"
#import <objc/runtime.h>

#define ScreenWidth   [[UIScreen mainScreen] bounds].size.width

static void *baiduMobAdPrerollNative_Key = &baiduMobAdPrerollNative_Key;

@implementation LCBaiduMobAdPrerollNative

@end

@implementation LCViewController (LCBaiduMobAdPrerollNative)

- (LCBaiduMobAdPrerollNative *)baiduMobAdPrerollNative {
    if (objc_getAssociatedObject(self, &baiduMobAdPrerollNative_Key) == nil) {
        LCBaiduMobAdPrerollNative *baiduMobAdPrerollNative = [[LCBaiduMobAdPrerollNative alloc] init];
        objc_setAssociatedObject(self, &baiduMobAdPrerollNative_Key, baiduMobAdPrerollNative, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
        return baiduMobAdPrerollNative;
    }
    return objc_getAssociatedObject(self, &baiduMobAdPrerollNative_Key);
}
//这里注意set后面的大小写
- (void)setBaiduMobAdPrerollNative:(LCBaiduMobAdPrerollNative *)baiduMobAdPrerollNative {
    objc_setAssociatedObject(self, &baiduMobAdPrerollNative_Key, baiduMobAdPrerollNative, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

//原生样式贴片，返回元素，自定义渲染
- (void)loadPrerollNative {
    //    [BaiduMobAdSetting sharedInstance].supportHttps = NO;
    if (!self.baiduMobAdPrerollNative.prerollNativeAd)
    {
        self.baiduMobAdPrerollNative.prerollNativeAd = [[BaiduMobAdPrerollNative alloc]init];
        self.baiduMobAdPrerollNative.prerollNativeAd.delegate = self;
        self.baiduMobAdPrerollNative.prerollNativeAd.publisherId = @"b3a6d662";
        self.baiduMobAdPrerollNative.prerollNativeAd.adId
        = @"5851516";//@"2058633";//需要为视频广告位
        self.baiduMobAdPrerollNative.prerollNativeAd.width = @(ScreenWidth);//@320;
        self.baiduMobAdPrerollNative.prerollNativeAd.height = @(ScreenWidth*9/12);//@240;
    }
    //请求广告
    [self.baiduMobAdPrerollNative.prerollNativeAd request];
}

- (BOOL)enableLocation {
    return YES;
}

- (void)didDismissLandingPage {
    NSLog(@"didDismissLandingPage");
    
}

#pragma BaiduMobAdPrerollNativeDelegate

- (void)onAdObjectsSuccessLoad:(NSArray *)prerollAds {
    BaiduMobAdPrerollNativeAdObject *object = [prerollAds objectAtIndex:0];
    float statusHeight = 64;
    if (ISIPHONEX) {
        statusHeight = 84;
    }
    self.baiduMobAdPrerollNative.nativeAdView = [self createPrerollNativeViewWithFrame:CGRectMake(0, statusHeight, ScreenWidth, ScreenWidth*9/12) object:object];
    if (![object isExpired]) {
        // 加载和显示广告内容
        
        [self.baiduMobAdPrerollNative.nativeAdView loadAndDisplayAdWithObject:object completion:^(NSArray *errors) {
            [self.baiduMobAdPrerollNative.nativeAdView trackImpression];
            [self.view addSubview:self.baiduMobAdPrerollNative.nativeAdView];
            
            if (object.materialType == NORMAL) {
                [self performSelector:@selector(timeAnimation)
                           withObject:self
                           afterDelay:1];
            }
            
        }];
    }
}

- (BaiduMobAdPrerollNativeView *)createPrerollNativeViewWithFrame:(CGRect)frame object:(BaiduMobAdPrerollNativeAdObject *)object {
    BaiduMobAdPrerollNativeView * nativeAdView = nil;
    
    UIImageView *baiduLogoView = [[UIImageView alloc]initWithFrame:CGRectMake(10, 220, 18, 18)];
    UIImageView *adLogoView = [[UIImageView alloc]initWithFrame:CGRectMake(286, 228, 24, 12)];
    
    
    if (object.materialType == VIDEO) {
        BaiduMobAdNativeVideoView *videoView = [[BaiduMobAdNativeVideoView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height) andObject:object];
        videoView.supportActImage = NO;
        videoView.supportControllerView = NO;
        nativeAdView = [[BaiduMobAdPrerollNativeView alloc]initWithFrame:frame videoView:videoView];
        NSTimeInterval currentTime = 0;
        [videoView sendVideoEvent:(onShow) currentTime:currentTime];
        
    } else if (object.materialType == NORMAL) {
        
        
        UIImageView *mainImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        nativeAdView = [[BaiduMobAdPrerollNativeView alloc]initWithFrame:frame mainImage:mainImageView];
        [self addTimeLabel:nativeAdView];
        
        
    } else if (object.materialType == GIF) {
        nativeAdView = [[BaiduMobAdPrerollNativeView alloc]initWithFrame:frame mainImage:nil];
        UIWebView *webview = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, frame.size.width, frame.size.height)];
        webview.backgroundColor = [UIColor redColor];
        NSString *htmlStr = [NSString stringWithFormat:@"%@", @"<html><body><img src='%@'/></body></html>"];
        NSString *imageHTML  = [[NSString alloc] initWithFormat:htmlStr, object.mainImageURLString];
        webview.userInteractionEnabled = NO;
        [webview loadHTMLString:imageHTML baseURL:[NSURL URLWithString:@"file://"]];
        [nativeAdView addSubview:webview];
        // 展现gif物料
    }
    nativeAdView.baiduLogoImageView = baiduLogoView;
    [nativeAdView addSubview:baiduLogoView];
    nativeAdView.adLogoImageView = adLogoView;
    [nativeAdView addSubview:adLogoView];
    
    
    return nativeAdView;
}


- (void)addTimeLabel:(UIView *)baseView {
    self.baiduMobAdPrerollNative.seconds = 6;
    CGRect rect =
    CGRectMake(baseView.frame.size.width - 30, 5, 20, 15);
    self.baiduMobAdPrerollNative.timeLabel = [[UILabel alloc] initWithFrame:rect] ;
    self.baiduMobAdPrerollNative.timeLabel.font = [self.baiduMobAdPrerollNative.timeLabel.font fontWithSize:12];
    self.baiduMobAdPrerollNative.timeLabel.textAlignment = NSTextAlignmentCenter;
    self.baiduMobAdPrerollNative.timeLabel.backgroundColor = [UIColor colorWithWhite:0.6 alpha:0.6];//bai色
    [baseView addSubview:self.baiduMobAdPrerollNative.timeLabel];
    self.baiduMobAdPrerollNative.timeLabel.text =
    [NSString stringWithFormat:@"%ld", (long)self.baiduMobAdPrerollNative.seconds];
    
}
- (void)timeAnimation {
    if (self.baiduMobAdPrerollNative.seconds > 1) {
        self.baiduMobAdPrerollNative.seconds--;
        self.baiduMobAdPrerollNative.timeLabel.text =
        [NSString stringWithFormat:@"%ld", (long)self.baiduMobAdPrerollNative.seconds];
        [self performSelector:@selector(timeAnimation)
                   withObject:self
                   afterDelay:1];
        //        [self amplitude];
    } else {
        [self.baiduMobAdPrerollNative.nativeAdView removeFromSuperview];
        [self didAdClose];
    }
}
- (void)onAdsFailLoad:(BaiduMobFailReason) reason {
    NSLog(@"didAdFailed");
}

- (void)onAdClicked:(BaiduMobAdBaseNativeAdView*)adView {
    
}

- (void)didDismissLandingPage:(BaiduMobAdBaseNativeAdView *)adView {
}

- (void)didAdClose {
    self.baiduMobAdPrerollNative.prerollNativeAd.delegate = nil;
    self.baiduMobAdPrerollNative.prerollNativeAd = nil;
}

@end
